/**
 * 用户管理js文件
 * @shanwang
 */
var userAid,userName,userData,checkedUserData,lastId=null;
/**
 * 用户数据表格属性配置
 */
$('#org_userGrid').datagrid({ 
	url:'user/getUsersByParamsService',
    singleSelect:true,
    rownumbers:true,
    columns:[[ 
        {field:'username',title:'用户名',width:'12%'},  
        {field:'name',title:'姓名',width:'18%'},
        /*{field:'gender',title:'性别',width:'18%'},*/
        {field:'group',title:'组别',width:'18%'},
        {field:'state',title:'状态',width:'18%'}, 
        {field:'option',title:'启用禁用',width:'10%',
        	formatter:function(value, row, index){
        		return 	'<a href="#" onclick="enableOrDisableUser(\''+row.username+'\',\''+'1'+'\',\''+row.state+'\')">启用</a>'
        		       +'||'+'<a href="#" onclick="enableOrDisableUser(\''+row.username+'\',\''+'0'+'\',\''+row.state+'\')">禁用</a>';
		}},
        {field:'option1',title:'重置密码',width:'10%',
        	formatter:function(value, row, index){
        		return 	'<a href="#" onclick="resetPassword(\''+row.username+'\')">重置密码</a>';
		}}
    ]],
    pagination:true,
    enableHeaderClickMenu: false,
    loadFilter:window.systemPage.selfLoadFilter,
    pagingMenu: false,
    selectOnRowContextMenu: true,
   /* rowContextMenu: [{	//	自定义右键菜单：添加、删除和修改
    	text: '添加',
    	iconCls: 'fa fa-plus color-icon',
    	handler: function() {
    		addUser();
    	}
    },{
    	text: '删除',
    	iconCls: 'fa fa-trash color-icon',
    	handler: function() {
    		deleteUser();
    	}
    },{
    	text	: '分配角色',
    	iconCls: 'fa fa-bell color-icon',
    	handler: function() {
    		distributeRole();
    	}
    },{
    	text	: '绑定人员',
    	iconCls: 'fa fa-user color-icon',
    	handler: function() {
    		addUserPerson();
    	}
    }]*/
});


/**
 * 查询用户
 */
function queryUser() {
	$('#org_userGrid').datagrid({
		url:'user/getUsersByParamsService',
		queryParams:{
			nickname:$('#user_nickname').val()
		},
		loadFilter:window.systemPage.selfLoadFilter
	});
}

/**
 * 添加用户
 */
function addUser() {
	$('#addUserForm').form('clear');
	$('#password-css').css({'display':'none'});
	$('input:radio[name="readonly"][value="0"]').prop('checked', true);
	$('#org_addUserWin').window('open');
}

/**
 * 添加用户保存
 */
function addUser_saveOK() {
	var jsonData = window.systemPage.getFormData('addUserForm');
	if($('#orgUser_password').val() != $('#orgUser_confirmPassword').val()) {
		$('#password-css').css({'display':'inline-block'});
	}else{
		var submitFlag = $('#addUserForm').form('validate');
		if(submitFlag==true) {
			$.ajax({
				url:'user/addUserServiceDD',
				type: 'post',
			    data: jsonData,
			    success: function() {
			    	$('#org_addUserWin').window('close');
			    	queryUser();
			    }
			});
		}else{
			$.messager.alert('我的消息','不能保存，还有必填项未填写或填写格式不正确！');
			return;
		}
	}
}
/**
 * 添加用户取消
 */
function addUser_cancelOK() {
	$('#org_addUserWin').window('close');
}

/**
 * 删除用户
 */
function enableOrDisableUser(username,newState,oldState){
	debugger;
	var state = "";
	var text = "";
	if(oldState == "启用"){
		state = "1";
	}else{
		state = "0";
	}
	if(newState == state) {
		$.messager.show({
			title: '提示',
			msg: '此用户已经被'+oldState+'了',
			position: 'bottomRight',
			shadow:false,
			timeout:2000
		});
	}else{
		if(newState == "1"){
			text = "启用";
		}else{
			text = "禁用";
		}
		$.messager.confirm('确认对话框', '您确定要'+text+'该用户吗？', function(r){
			if (r){
				$.ajax({
					url: 'user/enableOrDisableUserService',
					type: "post",  
			        data: {username:username,state:newState}, 
					success: function(data) {
						queryUser();
					},
				});
			}
		});
	}
}

/**
 * 重置密码
 * @param username
 */
function resetPassword(username){
	$.messager.confirm('确认','您确认要重置密码吗？',function(r){    
	    if (r){    
	    	$.post('user/resetUserPasswordService', {username : username}, function(data) {
	    		queryUser();
	    	});   
	    }    
	});
}

/**
 *启用禁用用户
 */
function deleteUser(){
	var rowData = $('#org_userGrid').datagrid('getSelected');
	if(rowData == null || rowData == undefined) {
		$.messager.show({
			title: '提示',
			msg: '至少要选择一条数据',
			position: 'bottomRight',
			shadow:false,
			timeout:2000
		});
	}else{
		$.messager.confirm('确认对话框', '您确定要删除该用户吗？', function(r){
			if (r){
				$.ajax({
					url: 'user/deleteUserService',
					type: "post",  
			        data: {username:rowData.username,aid:rowData.aid}, 
					success: function(data) {
						queryUser();
					},
				});
			}
		});
	}
}

/**
 * 分配角色
 */
function distributeRole(){
	var rowData = $('#org_userGrid').datagrid('getSelected');
	if(rowData == null || rowData == undefined) {
		$.messager.show({
			title: '提示',
			msg: '至少要选择一条数据',
			position: 'bottomRight',
			shadow:false,
			timeout:2000
		});
	}else{
		userAid = rowData.aid;
		userName = rowData.username;
		$('#org_addUserRoleWin').window('open');
		$('#org_addRoleGrid').datagrid({
    		url:'user/getRoleUserRefsService',
    		queryParams:{
    			aid:userAid,
    			username:userName
    		},
    		loadFilter:window.systemPage.selfLoadFilter,
    		onLoadSuccess:function(data) {
    			var thisData = data.rows;
				for(var i=0;i<thisData.length;i++) {
					if(thisData[i].username) {
						$('#org_addRoleGrid').datagrid('checkRow',i);
					}
				}
			}
    	});
	}
}

/**
 * 分配角色数据表格配置
 */
$('#org_addRoleGrid').datagrid({ 
    rownumbers:true,
    enableHeaderClickMenu: false,
    height:300,
    columns:[[ 
      	{field:'ck',checkbox:true},      
        {field:'rid',title:'rid',hidden:true},
        {field:'username',title:'username',hidden:true},
        {field:'rname',title:'角色名称',width:'30%'},  
        {field:'aid',title:'所属应用',hidden:true},  
        {field:'aname',title:'所属应用',width:'30%'}
    ]],
    pagingMenu: false
});
/**
 * 分配角色（保存）
 */
function addUserRole_saveOK(){
	var dataArr=[],checkedData = $('#org_addRoleGrid').datagrid('getChecked');
	for(var i=0;i<checkedData.length;i++) {
		var data = {};
		data.aid = checkedData[i].aid;
		data.username = userName;
		data.rid = checkedData[i].rid;
		dataArr.push(data);
	}
	$.ajax({
		url:'user/updateRoleUserRefsService',
		type:'post',
		contentType : 'application/json;charset=utf-8', //设置请求头信息
		data:JSON.stringify({aid:userAid, username:userName, roleUserRefDatas:dataArr}),
		success:function(data) {
			$('#org_addUserRoleWin').window('close');
			queryUser();
		}
	});
}

